<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/


Route::get('/wechat', 'WeixinController@validateWechatToken');
Route::post('/wechat', 'WeixinController@responseMsg');


Route::group(['middleware' => ['web']], function () {
    Route::auth();
    Route::get('loginByGithub', 'SocialController@loginByGithub');
    Route::get('authGithub', 'SocialController@authGithub');

    Route::get('loginByWeibo', 'SocialController@loginByWeibo');
    Route::get('authWeibo', 'SocialController@authWeibo');

    Route::post('/wechat', 'WeixinController@responseMsg');


    //image
    Route::get('image/fetch', 'ImageController@fetchImageBy');
    Route::get('image/fixImageAltDescription', 'ImageController@fixImageAltDescription');
    Route::post('image/callback', 'ImageController@callBack');//在csrf保护中已近排除了!!!!
    Route::get('image/qiniuIndex', 'ImageController@qiniuIndex');
    Route::resource('image', 'ImageController');
    Route::get('getImagesSizeInfo', 'ImageController@getImagesSizeInfo');
    Route::get('imageQuote', 'ImageController@imageQuote');


    //tag
    Route::resource('tag', 'TagController');

    //ep
    Route::get('episodeByTalkShow', 'EpisodeController@episodeByTalkShow');
    Route::get('episodeByBaidduPan', 'EpisodeController@episodeByBaidduPan');
    Route::get('episodeByBritain', 'EpisodeController@episodeByBritain');
    Route::get('episodeByCooked', 'EpisodeController@episodeByCooked');
    Route::get('episodeByDocumentary', 'EpisodeController@episodeByDocumentary');
    Route::get('episodeByMovie', 'EpisodeController@episodeByMovie');
    Route::get('episodeByJapan', 'EpisodeController@episodeByJapan');
    Route::get('episodeByKorean', 'EpisodeController@episodeByKorean');
    Route::get('episodeByThailand', 'EpisodeController@episodeByThailand');

    Route::get('episode/recentlyUpdated', 'EpisodeController@recentlyUpdatedEpisodes');
    Route::resource('episode', 'EpisodeController');

    Route::get('/', 'EpisodeController@recentlyUpdatedEpisodes');


    ///article/set-coverage

    Route::get('hotArticles', 'ArticleController@hotArticles');
    //hotArticles
    Route::resource('article', 'ArticleController');
    Route::get('show/{show}/relatedArticles', 'ArticleController@showRelatedArticles');
    Route::post('article/publishTriggerAction', 'ArticleController@publishTriggerAction');//发表文章
    Route::post('article/set-coverage', 'ArticleController@setCoverage');//发表文章
    //发送网站文章到微博
    Route::get('article/publicToWeibo/{article}', 'ArticleController@publicToWeibo');

    //发送网站文章到头条
    Route::get('article/publicToToutiao/{article}', 'ArticleController@publicToToutiao');

    Route::get('article/edit-wang/{article}', 'ArticleController@editWang');

    //show
    Route::get('hotShows', 'ShowController@hotShows');
    Route::resource('show', 'ShowController');

    Route::get('search', 'SearchController@search');
    Route::get('showTextDetail/{show}', 'ShowController@textDetail');
    Route::get('swapShow/{show}', 'ShowController@swapShow');

    Route::post('swapShow', 'ShowController@swapShowUpdate');
    Route::post('nameOfShow', 'ShowController@nameOfShow');
    Route::post('createShowWithEpisode', 'ShowController@createShowWithEpisode');
    Route::post('createMovieWithEpisode', 'ShowController@createMovieWithEpisode');


    Route::get('showSortedRecentlyUpdated', 'ShowController@recentlyUpdatedShows');
    Route::get('showSortedInWall', 'ShowController@latestInWallShow');
    Route::get('showSortedOutWall', 'ShowController@latestOutWallShow');

    //movie
    Route::get('imgur/index', 'ImgurController@index');
    Route::post('imgur/update', 'ImgurController@update');
    Route::get('imgur/edit/{imgur}', 'ImgurController@edit');
    Route::get('imgur/delete/{imgur}', 'ImgurController@delete');

    Route::resource('movie', 'MovieController');
    Route::get('movie/{movie}/swap', ['as' => 'movie.swap', 'uses' => 'MovieController@swap']);
    Route::get('movie/{value}/{key}/smartCreate', 'MovieController@smartCreate');
    Route::get('hotMovies', 'MovieController@hotMovies');
    Route::post('movie/swapStore', ['as' => 'movie.swapStore', 'uses' => 'MovieController@swapStore']);
    Route::get('createMovieBySelectEpsInShow/{show}', 'MovieController@createMovieBySelectEp');
    Route::post('/movie/selectedEpsToMovies', 'MovieController@selectedEpsToMovies');
    Route::get('/moveEpToMovieContainer', 'MovieController@moveEpToMovieContainer');
    Route::get('/movieWithoutDescription', 'MovieController@movieWithoutDescription');
    Route::get('/movieWithoutImage', 'MovieController@movieWithoutImage');
    Route::post('swapMovieEpByName', 'MovieController@swapMovieEpByName');


    //文章 vote
    Route::post('vote/forArticle', 'VoteController@forArticle');
    Route::post('vote/forMovie', 'VoteController@forMovie');
    Route::post('vote/forShow', 'VoteController@forShow');
    Route::post('vote/forEpisode', 'VoteController@forEpisode');


    Route::get('quoteOneKeyCreate', 'QuoteController@quoteOneKeyCreate');

    //getLatesWeiboOfMine
    Route::get('weibo', 'WeiboController@index');
    Route::get('weiboImages', 'WeiboController@weiboImages');
    Route::get('weibo/isTrash/{weibo}', 'WeiboController@isTrash');
    Route::get('weibo/isValue/{weibo}', 'WeiboController@isValue');


    //ueditor

    route::get('ueditor/listimage', 'UeditorController@listimage');
    route::get('ueditor/searchimage', 'UeditorController@searchimage');
    route::post('ueditor/uploadimage', 'UeditorController@uploadimage');
    route::get('ueditor/catchimage', 'UeditorController@catchimage');
    //toutiao
    route::get('auth/toutiao', 'Auth\AuthController@toutiaoAuth');
    route::get('auth/toutiao-callback', 'Auth\AuthController@toutiaoCallback');

//视频解析
    route::post('/api/video-parse', 'ApiController@index');
    route::get('/api/video-parse', 'ApiController@index');

});

/**
 * 创建网站sitemap
 */
Route::group(['prefix' => 'sitemap'], function () {

    Route::get('index.xml', function () {
        $capability = 2000;
        $sitemap = App::make("sitemap");
        $sitemap->setCache('trytv.sitemap-index', 3600 * 3);

        if (!$sitemap->isCached()) {
            //经常访问
            $sitemap->addSitemap(URL::to('sitemap/daily.xml'));

            //美剧
            $count = \App\Episode::count();
            $page = intval(ceil($count / $capability));
            for ($i = 1; $i <= $page; $i++) {
                $end = $i * $capability;
                $start = $end - $capability;
                $url = "sitemap/episode_{$start}_{$end}.xml";
                $sitemap->addSitemap(URL::to($url));
            }
            //show 剧集
            $count = \App\Show::count();
            $page = intval(ceil($count / $capability));
            for ($i = 1; $i <= $page; $i++) {
                $end = $i * $capability;
                $start = $end - $capability;
                $url = "sitemap/show_{$start}_{$end}.xml";
                $sitemap->addSitemap(URL::to($url));
            }
            //文章 aritcle
            $count = \App\Article::count();
            $page = intval(ceil($count / $capability));
            for ($i = 1; $i <= $page; $i++) {
                $end = $i * $capability;
                $start = $end - $capability;
                $url = "sitemap/article_{$start}_{$end}.xml";
                $sitemap->addSitemap(URL::to($url));
            }
        }

        return $sitemap->render('sitemapindex');

    });

    Route::get('episode_{start}_{end}.xml', function ($start, $end) {
        $sitemap = App::make("sitemap");
        $sitemap->setCache("episode-{$start}-{$end}", 3600 * 24);
        if (!$sitemap->isCached()) {
            $episodes = \App\Episode::where('id', '>=', $start)->where('id', '<', $end)->orderBy('created_at', 'desc')->get();
            foreach ($episodes as $episode) {
                $sitemap->add(url('/episode', $episode), $episode->updated_at, '0.1', 'monthly');
            }
        }

        return $sitemap->render('xml');
    });

    Route::get('show_{start}_{end}.xml', function ($start, $end) {
        $sitemap = App::make("sitemap");
        $sitemap->setCache("show-{$start}-{$end}", 3600 * 24);
        if (!$sitemap->isCached()) {
            $shows = \App\Show::where('id', '>=', $start)->where('id', '<', $end)->orderBy('created_at', 'desc')->get();
            foreach ($shows as $show) {
                $sitemap->add(url('/show', $show), $show->updated_at, '0.1', 'weekly');
            }

        }
        return $sitemap->render('xml');
    });

    Route::get('article_{start}_{end}.xml', function ($start, $end) {
        $sitemap = App::make("sitemap");
        $sitemap->setCache("article-{$start}-{$end}", 3600 * 24);
        if (!$sitemap->isCached()) {

            $articles = \App\Article::where('id', '>=', $start)->where('id', '<', $end)->orderBy('created_at', 'desc')->get();
            foreach ($articles as $article) {
                $sitemap->add(url('/article', $article), $article->updated_at, '0.3', 'weekly');
            }
        }

        return $sitemap->render('xml');
    });

    Route::get('daily.xml', function () {
        $sitemap = App::make("sitemap");
        $sitemap->setCache('sitemap_daily', 3600 * 24);
        if (!$sitemap->isCached()) {
            $sitemap->add(url('/'), \Carbon\Carbon::now(), '1', 'daily');
            $sitemap->add(url('/hotShows'), \Carbon\Carbon::now(), '1', 'daily');
            $sitemap->add(url('/episode/recentlyUpdated'), \Carbon\Carbon::now(), '1', 'daily');
            $sitemap->add(url('/episode/type/TalkShow'), \Carbon\Carbon::now(), '1', 'daily');
        }
        return $sitemap->render('xml');
    });

});


Route::get('movieAjax/ajaxTest', 'MovieController@ajaxTest');


Route::get('console/test', function () {
    //(new \App\Console\Commands\SpiderAssrt())->handle();
    (new \App\Console\Commands\ImgurFetcher())->handle();
});
